package com.tudouji.project.mt.service.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.tudouji.common.enums.InFormChannelEnums;
import com.tudouji.common.enums.OutFormChannelEnums;
import com.tudouji.common.exception.CustomException;
import com.tudouji.common.utils.DateUtils;
import com.tudouji.common.utils.IdUtils;
import com.tudouji.common.utils.StringUtils;
import com.tudouji.project.bill.domain.*;
import com.tudouji.project.bill.mapper.*;
import com.tudouji.project.bill.service.impl.WhoutformServiceImpl;
import com.tudouji.project.customer.domain.Customer;
import com.tudouji.project.customer.mapper.CustomerMapper;
import com.tudouji.project.mt.domain.*;
import com.tudouji.project.mt.mapper.MtSalesItemsMapper;
import com.tudouji.project.mt.service.IMtShopService;
import com.tudouji.project.mt.service.IMtTokenService;
import com.tudouji.project.organization.domain.Organization;
import com.tudouji.project.organization.mapper.OrganizationMapper;
import com.tudouji.project.system.domain.SysDept;
import com.tudouji.project.system.domain.SysUser;
import com.tudouji.project.system.mapper.SysDeptMapper;
import com.tudouji.project.system.service.impl.BusinesscodeServiceImpl;
import com.tudouji.project.system.service.impl.SysConfigServiceImpl;
import com.tudouji.project.system.service.impl.SysUserServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tudouji.project.mt.mapper.MtSalesMapper;
import com.tudouji.project.mt.service.IMtSalesService;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import static com.tudouji.project.mt.domain.MtShop.sell_state;
import static com.tudouji.project.mt.domain.MtShop.whin_state;

/**
 * 美团门店菜品销售主Service业务层处理
 * 
 * @author tudouji
 * @date 2023-12-04
 */
@Slf4j
@Service
public class MtSalesServiceImpl implements IMtSalesService 
{
    @Autowired
    private MtSalesMapper mtSalesMapper;
    @Autowired
    private MtSalesItemsMapper mtSalesItemsMapper;
    @Autowired
    private IMtShopService mtShopService;
    @Autowired
    private SysConfigServiceImpl sysConfigService;
    @Autowired
    private OrganizationMapper organizationMapper;
    @Autowired
    private SysUserServiceImpl sysUserService;
    @Autowired
    private SysDeptMapper sysDeptMapper;
    @Autowired
    private BusinesscodeServiceImpl businesscodeService;
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private ProducereportMapper producereportMapper;
    @Autowired
    private ProducereportitemMapper producereportitemMapper;
    @Autowired
    private BusinesscodeServiceImpl codeService;
    @Autowired
    private WhoutformMapper whoutformMapper;
    @Autowired
    private WhoutformitemMapper whoutformitemMapper;
    @Autowired
    private WhinformMapper whinformMapper;
    @Autowired
    private WhinformitemMapper whinformitemMapper;
    @Autowired
    private MtWhinServiceImpl mtWhinService;
    @Autowired
    private MtWhoutServiceImpl mtWhoutService;
    @Autowired
    private WhoutformServiceImpl whoutformService;
    @Autowired
    private MtPullService mtPullService;
    @Autowired
    private IMtTokenService iMtTokenService;

    /**
     * 查询美团门店菜品销售主
     * 
     * @param id 美团门店菜品销售主ID
     * @return 美团门店菜品销售主
     */
    @Override
    public MtSales selectMtSalesById(Long id)
    {
        return mtSalesMapper.selectMtSalesById(id);
    }

    /**
     * 查询美团门店菜品销售主列表
     * 
     * @param mtSales 美团门店菜品销售主
     * @return 美团门店菜品销售主
     */
    @Override
    public List<MtSales> selectMtSalesList(MtSales mtSales)
    {
        return mtSalesMapper.selectMtSalesList(mtSales);
    }
    @Override
    public MtSales selectMtSalesByDishName(MtSales mtSales)
    {
        return mtSalesMapper.selectMtSalesByDishName(mtSales);
    }
    @Override
    public List<MtSales> selectMtSalesListPC(MtSales mtSales)
    {
        return mtSalesMapper.selectMtSalesListPC(mtSales);
    }

    /**
     * 新增美团门店菜品销售主
     * 
     * @param mtSales 美团门店菜品销售主
     * @return 结果
     */
    @Override
    public int insertMtSales(MtSales mtSales)
    {
        mtSales.setCreateTime(DateUtils.getNowDate());
        return mtSalesMapper.insertMtSales(mtSales);
    }

    /**
     * 修改美团门店菜品销售主
     * 
     * @param mtSales 美团门店菜品销售主
     * @return 结果
     */
    @Override
    public int updateMtSales(MtSales mtSales)
    {
        mtSales.setUpdateTime(DateUtils.getNowDate());
        return mtSalesMapper.updateMtSales(mtSales);
    }

    /**
     * 批量删除美团门店菜品销售主
     * 
     * @param ids 需要删除的美团门店菜品销售主ID
     * @return 结果
     */
    @Override
    public int deleteMtSalesByIds(Long[] ids)
    {
        return mtSalesMapper.deleteMtSalesByIds(ids);
    }

    /**
     * 删除美团门店菜品销售主信息
     * 
     * @param id 美团门店菜品销售主ID
     * @return 结果
     */
    @Override
    public int deleteMtSalesById(Long id)
    {
        return mtSalesMapper.deleteMtSalesById(id);
    }

    @Override
    @Transactional
    public void dataPush(List<MtShop> mtShopList) {
        for (MtShop shop : mtShopList) {

            int pushDays = shop.getPushDays();
            HashMap<String, Date> PUSH_DATE = mtShopService.getPushDate(shop);
            Date bizTimeLastEnd = PUSH_DATE.get("bizTimeLastEnd");
            Date bizTimeEnd = PUSH_DATE.get("bizTimeEnd");
            Date bizTimeStart = PUSH_DATE.get("bizTimeStart");

            while (0 < DateUtils.getDateDiff(bizTimeLastEnd, bizTimeEnd)) {

                //1、销售订单明细表中的物料数据生成领料出库报表（preduceReport），按日、按门店、按菜品、按物料汇总生成，
                // 成本对象记录为菜品的ERP物料id，每个门店每天每个菜品一张领料出库单
                MtSales mtSales = new MtSales();
                mtSales.setMtShopid(shop.getId());
                mtSales.setBizTimeStart(bizTimeStart.getTime());
                mtSales.setBizTimeEnd(bizTimeEnd.getTime());
                mtSales.setStatusNo(new Integer[]{50});
                List<MtSales> mtSalesList = mtSalesMapper.selectMtSalesList(mtSales);
                if (mtSalesList.size() > 0){
                    log.info("数据未处理完成！");
                    return;
                }

                mtSales.setStatusNo(null);
                mtSales.setStatus(50);
                List<MtSales> produceReports = mtSalesMapper.dataForProduceReport(mtSales);
                if (produceReports.size() == 0){
                    log.info("没有数据需要处理！");
                    //4. 更新起始日期
                    bizTimeStart = DateUtils.addSeconds(bizTimeEnd, 1);
                    bizTimeEnd = DateUtils.addDay(bizTimeEnd, pushDays);
                    continue;
                }

                String businesscode = codeService.getCode("37");
                //领料出库报表
                createProduceReport(bizTimeStart, bizTimeEnd, produceReports, businesscode, shop.getSysuserid());
                //领料出库单
                List<MtSalesItems> mtSalesItems = mtSalesMapper.dataForPickinglistOut(mtSales);
                createPicklistOut(mtSalesItems, 4, bizTimeEnd, businesscode, shop.getSysuserid());
                //2、销售订单主表中的菜品数据生成销售出库单，按日、按门店汇总生成，每个门店每天一张销售出库单。
//                List<MtSalesItems> items = mtSalesItemsMapper.dataforSellOut(mtSales);
                List<MtSalesItems> whinItems = mtSalesItemsMapper.dataforProductWhin(mtSales);
                createSellOut(whinItems, 2, bizTimeEnd, businesscode, shop.getSysuserid());

                //3、销售订单主表中的菜品数据生成产品入库单，按日、按门店汇总生成，每个门店每天一张产品入库单。
                createProductWhin(whinItems, bizTimeEnd, businesscode, shop.getSysuserid());

                //更新美团销售订单状态
                for (MtSales sales:produceReports){
                    MtSales salesUp = new MtSales();
                    salesUp.setId(sales.getId());
                    salesUp.setStatus(100);
                    salesUp.setCode(businesscode);
                    mtSalesMapper.updateMtSales(salesUp);
                }
                //4. 更新起始日期
                bizTimeStart = DateUtils.addSeconds(bizTimeEnd, 1);
                bizTimeEnd = DateUtils.addDay(bizTimeEnd, pushDays);
            }

            mtShopService.updateShopByPush(shop, sell_state ,bizTimeEnd);
        }
    }

    private void createProductWhin(List<MtSalesItems> items, Date bizTimeEnd, String businesscode, Long sysuserid) {
        SysUser sysUser = sysUserService.selectUserById(sysuserid);

        MtSalesItems mtSalesItems = items.get(0);
        String guid = IdUtils.fastSimpleUUID();
        Organization organization = organizationMapper.selectOrganizationByAutoId(mtSalesItems.getOrgId());
        Organization mtOrganization = organizationMapper.selectOrganizationByAutoId(mtSalesItems.getMtOrgId());
        SysDept department = sysDeptMapper.selectDeptById(mtSalesItems.getDeptId());
        String orgguid = organization.getGuid();
        //组装入库单
        Whinform form = new Whinform();
        form.setChannel(InFormChannelEnums.MT.getCode());
        form.setCreatedate(DateUtils.getNowDate());
        form.setFoguid(businesscode);
        form.setBusinesscode(businesscode);
        form.setDepartmentid(department.getGuid());
        form.setStorermguid(mtSalesItems.getInStoreroomguid());
        form.setOrgguid(orgguid);
        form.setOrgname(mtSalesItems.getOrgName());
        form.setWhintype(4);
        form.setCurrency(1);
        form.setSettlOrgId(mtOrganization.getGuid());
        form.setSettlOrgName(mtOrganization.getName());
        form.setFormno(businesscodeService.getCode("4"));
        form.setGuid(guid);
        form.setCreateTime(DateUtils.getNowDate());
        form.setIsCopytoKD(1);
        form.setOperatorid(sysUser.getTuserId());
        whinformMapper.insertWhinform(form);

        for (MtSalesItems mtItem : items) {
            BigDecimal weight = new BigDecimal(0);
            Whinformitem item = new Whinformitem();
            item.setGuid(IdUtils.fastSimpleUUID());
            item.setWhinid(guid);
            item.setSrguid(mtItem.getInStoreroomguid());
            item.setShrguid(mtItem.getInStoreHouseguid());
            item.setProductguid(mtItem.getInproductid());
            item.setSpguid(mtItem.getInspecid());
            item.setOrgguid(form.getOrgguid());
            item.setOrgname(form.getOrgname());
            item.setWeight(item.getWeight());
            item.setStockunitid(mtItem.getInstockunitid());
            item.setStockunit(mtItem.getInstockunit());
            item.setWeight(mtItem.getInErpWeight());
            item.setAmount(mtItem.getInAmount());
            item.setPrice(mtItem.getInErpPrice());
            item.setCreateTime(DateUtils.getNowDate());
            whinformitemMapper.insertWhinformitem(item);
            weight = weight.add(item.getWeight());
            form.setWeight(weight);
            form.setGuid(null);
            whinformMapper.updateWhinform(form);
            //更新库存
            item.setSpename(mtItem.getInspecname());
            mtWhinService.updateWhinfo(item);
            //更新动态库存
            whoutformService.syncWhinfoRecords(item, bizTimeEnd, bizTimeEnd);
        }

    }

    private void createPicklistOut(List<MtSalesItems> items, Integer outtype, Date bizTimeEnd, String businesscode
                , Long sysuserid) {
        SysUser sysUser = sysUserService.selectUserById(sysuserid);

        MtSalesItems mtSalesItems = items.get(0);
        String guid = IdUtils.fastSimpleUUID();
        Organization organization = organizationMapper.selectOrganizationByAutoId(mtSalesItems.getOrgId());
        MtShop mtShop = mtShopService.selectMtShopById(mtSalesItems.getMtShopId());
        Customer customer = customerMapper.selectCustomerByNewId(mtShop.getCustomerId());
        SysDept department = sysDeptMapper.selectDeptById(mtSalesItems.getDeptId());
        String orgguid = organization.getGuid();

        Whoutform whoutform = new Whoutform();
        whoutform.setChannel(OutFormChannelEnums.MT.getCode());
        whoutform.setGuid(guid);
        whoutform.setOrderguid(businesscode);
        whoutform.setBusinesscode(businesscode);
        whoutform.setIsCopytoKD(1);
        whoutform.setDepartmentid(department.getGuid());
        whoutform.setOuttype(outtype);
        whoutform.setSrguid(mtSalesItems.getStoreroomguid());
        whoutform.setSettlOrgId(orgguid);
        whoutform.setSettlOrgName(mtSalesItems.getOrgName());
        whoutform.setRevCustId(customer.getGuid());
        whoutform.setRevCustName(mtSalesItems.getCustomerName());
        whoutform.setCurrency(1);
        whoutform.setOuttime(new Date());
        whoutform.setOrgguid(orgguid);
        whoutform.setOrgname(mtSalesItems.getOrgName());
        whoutform.setWhoutcode(codeService.getCode("7"));
        whoutform.setCreatetime(DateUtils.getNowDate());
        whoutform.setOperatorid(sysUser.getTuserId());
        whoutformMapper.insertWhoutform(whoutform);

        BigDecimal weight = new BigDecimal(0);
        for (MtSalesItems item : items) {
            // 保存明细
            Whoutformitem whoutformitem = new Whoutformitem();
            whoutformitem.setGuid(IdUtils.fastSimpleUUID());
            whoutformitem.setWhoutid(guid);
            whoutformitem.setSrguid(item.getStoreroomguid());
            whoutformitem.setShrguid(item.getStorehouseguid());
            whoutformitem.setProductguid(item.getProductguid());
            whoutformitem.setSpguid(item.getSpguid());
            whoutformitem.setOrgguid(orgguid);
            whoutformitem.setOrgname(item.getOrgName());
            whoutformitem.setWeight(item.getErpWeight());
            whoutformitem.setPrice(item.getErpPrice());
            whoutformitem.setStockunitid(item.getStockunitid());
            whoutformitem.setStockunit(item.getStockunitname());
            if (outtype == 4){
                whoutformitem.setCospguid(item.getInspecid());
            }
            whoutformitem.setCreateTime(DateUtils.getNowDate());
//            whoutformitem.setCreateBy(SecurityUtils.getLoginUser().getUser().getUserId());
            whoutformitemMapper.insertWhoutformitem(whoutformitem);
            weight = weight.add(whoutformitem.getWeight());
            whoutform.setWeight(weight);
            whoutformMapper.updateWhoutform(whoutform);
            //更新库存
            whoutformitem.setSpename(item.getSpname());
            mtWhoutService.updateWhinfo(whoutformitem);
            //更新动态库存
            whoutformService.syncWhinfoRecords(whoutformitem, bizTimeEnd);
        }
    }

    private void createSellOut(List<MtSalesItems> items, Integer outtype, Date bizTimeEnd, String businesscode
            ,Long sysuserid) {
        SysUser sysUser = sysUserService.selectUserById(sysuserid);

        MtSalesItems mtSalesItems = items.get(0);
        String guid = IdUtils.fastSimpleUUID();
        Organization organization = organizationMapper.selectOrganizationByAutoId(mtSalesItems.getOrgId());
        MtShop mtShop = mtShopService.selectMtShopById(mtSalesItems.getMtShopId());
        Customer customer = customerMapper.selectCustomerByNewId(mtShop.getCustomerId());
        SysDept department = sysDeptMapper.selectDeptById(mtSalesItems.getDeptId());
        String orgguid = organization.getGuid();

        Whoutform whoutform = new Whoutform();
        whoutform.setChannel(OutFormChannelEnums.MT.getCode());
        whoutform.setGuid(guid);
        whoutform.setOrderguid(businesscode);
        whoutform.setBusinesscode(businesscode);
        whoutform.setIsCopytoKD(1);
        whoutform.setDepartmentid(department.getGuid());
        whoutform.setOuttype(outtype);
        whoutform.setSrguid(mtSalesItems.getInStoreroomguid());
        whoutform.setSettlOrgId(orgguid);
        whoutform.setSettlOrgName(mtSalesItems.getOrgName());
        whoutform.setRevCustId(customer.getGuid());
        whoutform.setRevCustName(mtSalesItems.getCustomerName());
        whoutform.setCurrency(1);
        whoutform.setOuttime(new Date());
        whoutform.setOrgguid(orgguid);
        whoutform.setOrgname(mtSalesItems.getOrgName());
        whoutform.setWhoutcode(codeService.getCode("7"));
        whoutform.setCreatetime(DateUtils.getNowDate());
        whoutform.setOperatorid(sysUser.getTuserId());
        whoutformMapper.insertWhoutform(whoutform);

        BigDecimal weight = new BigDecimal(0);
        for (MtSalesItems item : items) {
            // 保存明细
            Whoutformitem whoutformitem = new Whoutformitem();
            whoutformitem.setGuid(IdUtils.fastSimpleUUID());
            whoutformitem.setWhoutid(guid);
            whoutformitem.setSrguid(item.getInStoreroomguid());
            whoutformitem.setShrguid(item.getInStoreHouseguid());
            whoutformitem.setProductguid(item.getInproductid());
            whoutformitem.setSpguid(item.getInspecid());
            whoutformitem.setOrgguid(orgguid);
            whoutformitem.setOrgname(item.getOrgName());
            whoutformitem.setWeight(item.getInErpWeight());
            whoutformitem.setPrice(item.getInErpPrice());
            whoutformitem.setStockunitid(item.getInstockunitid());
            whoutformitem.setStockunit(item.getInstockunit());
            if (outtype == 4){
                whoutformitem.setCospguid(item.getInspecid());
            }
            whoutformitem.setCreateTime(DateUtils.getNowDate());
//            whoutformitem.setCreateBy(SecurityUtils.getLoginUser().getUser().getUserId());
            whoutformitemMapper.insertWhoutformitem(whoutformitem);
            weight = weight.add(whoutformitem.getWeight());
            whoutform.setWeight(weight);
            whoutformMapper.updateWhoutform(whoutform);
            //更新库存
            whoutformitem.setSpename(item.getInspecname());
            mtWhoutService.updateWhinfo(whoutformitem);
            //更新动态库存
            whoutformService.syncWhinfoRecords(whoutformitem, bizTimeEnd);
        }
    }

    /**
     * 创建领料出库单
     * @param bizTimeStart
     * @param bizTimeEnd
     * @param mtSalesItems
     */
    private void createProduceReport(Date bizTimeStart, Date bizTimeEnd, List<MtSales> mtSalesItems, String businesscode
            ,Long sysuserid) {
        SysUser sysUser = sysUserService.selectUserById(sysuserid);

        for (MtSales mtSales : mtSalesItems) {
            Organization organization = organizationMapper.selectOrganizationByAutoId(mtSales.getOrgId());
            SysDept department = sysDeptMapper.selectDeptById(mtSales.getDeptId());
            String orgguid = organization.getGuid();

            Producereport producereport = new Producereport();
            producereport.setPlcode(businesscode);
            producereport.setOrgname(mtSales.getOrgName());
            producereport.setOrgguid(orgguid);
            producereport.setInweight(mtSales.getErpWeight());
            producereport.setStarttime(bizTimeStart);
            producereport.setEndtime(bizTimeEnd);
            producereport.setDepartid(department.getGuid());
            producereport.setDepartname(department.getDeptName());
            producereport.setSrguid(mtSales.getStoreroomguid());
            producereport.setSrname(mtSales.getStoreroomname());
            producereport.setOrderdate(bizTimeStart);
            producereport.setIntype(1);
            producereport.setCreattime(DateUtils.getNowDate());
            producereport.setOperatorid(sysUser.getTuserId());
            producereport.setPlstatus(105L);
            producereport.setPldate(bizTimeStart);
            producereportMapper.insertProducereport(producereport);

            BigDecimal outweight = BigDecimal.ZERO;
            List<MtSalesItems> mtSalesItemsList = getMtSalesItems(bizTimeStart, bizTimeEnd, mtSales);

            for (int i = 0; i < mtSalesItemsList.size(); i++) {
                MtSalesItems saleItem =  mtSalesItemsList.get(i);
                outweight = outweight.add(saleItem.getErpWeight());
            }

            for (int i = 0; i < mtSalesItemsList.size(); i++) {

                MtSalesItems saleItem =  mtSalesItemsList.get(i);

                Producereportitem ppItem = new Producereportitem();
                ppItem.setProducereportid(producereport.getId());
                ppItem.setDepartname(department.getDeptName());
                ppItem.setDepartid(department.getGuid());
                ppItem.setSrname(saleItem.getStoreroomname());
                ppItem.setSrguid(saleItem.getStoreroomguid());
                ppItem.setShrname(saleItem.getStorehousename());
                ppItem.setShrguid(saleItem.getStorehouseguid());
                ppItem.setOutproduct(saleItem.getProductname());
                ppItem.setOutproductid(saleItem.getProductguid());
                ppItem.setOutspecname(saleItem.getSpname());
                ppItem.setOutspecid(saleItem.getSpguid());
                ppItem.setInproduct(saleItem.getInproduct());
                ppItem.setInproductid(saleItem.getInproductid());
                ppItem.setInspecname(saleItem.getInspecname());
                ppItem.setInspecid(saleItem.getInspecid());
                ppItem.setInsptype(1);
                ppItem.setInweight(saleItem.getErpWeight().divide(outweight, 6, RoundingMode.HALF_UP).multiply(saleItem.getInweight()));
                ppItem.setOutweight(saleItem.getErpWeight());
                ppItem.setInstockunit(saleItem.getInstockunit());
                ppItem.setInstockunitid(saleItem.getInstockunitid());
                ppItem.setCospguid(saleItem.getInspecid());
                ppItem.setOutstockunitid(saleItem.getStockunitid());
                ppItem.setOutstockunit(saleItem.getStockunitname());
                producereportitemMapper.insertProducereportitem(ppItem);
            }
            Producereport updateReport = new Producereport();
            updateReport.setId(producereport.getId());
            updateReport.setOutweight(outweight);
            producereportMapper.updateProducereport(updateReport);
        }
    }

    private List<MtSalesItems> getMtSalesItems(Date bizTimeStart, Date bizTimeEnd, MtSales mtSales) {
        MtSales sales = new MtSales();
        sales.setMtShopid(mtSales.getMtShopid());
        sales.setBizTimeStart(bizTimeStart.getTime());
        sales.setBizTimeEnd(bizTimeEnd.getTime());
        sales.setStatus(50);
        sales.setOrgId(mtSales.getOrgId());
        sales.setDeptId(mtSales.getDeptId());
        sales.setStorehouseguid(mtSales.getStorehouseguid());
        sales.setSpguid(mtSales.getSpguid());

        List<MtSalesItems> mtSalesItemsList = mtSalesMapper.dataForProduceReportItems(sales);
        return mtSalesItemsList;
    }

    @Override
    @Transactional
    public void dataPull(List<MtShop> mtShopList) {
        log.info("销售单拉取"+this.getClass() + DateUtils.getTime());
//        Date yestoday = DateUtils.addDays(DateUtils.getNowDate(), -1);
        MtToken mtToken = iMtTokenService.getLastTokenByConditions(new MtToken());

        for (MtShop shop : mtShopList) {
            //获取当前店铺与昨天的天数差
//            long dateDiff = DateUtils.getDateDiff(yestoday, shop.getPulldate());
//            for (int i = 0; i < dateDiff; i++) {

            try {
                dataPullWithShop(mtToken, shop);
            } catch (Exception e) {
                throw new CustomException(e.getMessage());
            }
            String autoroll = sysConfigService.selectConfigByKey("mtpulldate_autoroll");
                if(StringUtils.isNotBlank(autoroll)&&autoroll.equalsIgnoreCase("false")){
                    log.info("美团拉取数据日期不自动滚动！");
                    return;
                }
//            }
            log.info("销售单拉取"+this.getClass() + DateUtils.getTime());
        }
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public void dataPullWithShop(MtToken mtToken, MtShop shop)  throws Exception {
        Date pulldate = shop.getPulldate();
        Long timeBegin = pulldate.getTime();
        Long timeEnd = DateUtils.getLastDayOfDate(pulldate).getTime();

        //获取MT接口数据
        mtPullService.getSale(shop, timeBegin, timeEnd, mtToken);
        //转换为业务实体，并插入,如果已存在对应ID的实体，则跳过

        shop = mtShopService.selectMtShopById(shop.getId());
        if (!shop.isPullFinished(sell_state)){
            shop.setPullState(sell_state, 1);
        }
        mtShopService.checkAndInitPullDate(shop);
        mtShopService.updateMtShop(shop);
    }
}
